package scantask.client.presentationLayer;

import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.layers.AirspaceLayer;
import gov.nasa.worldwind.layers.AnnotationLayer;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder;

import scantask.client.domainLayer.ControllerInterface;

public class FunctionalityPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// The world window of NASA.
	final private WorldWindow _wwd;
	
	// The client controller.
	private ControllerInterface _controller;
	
	// Layer for date.
	private AnnotationLayer _datesLayer;
	
	// Layers for shapes.
	private Map<String, AirspaceLayer> _layers;
	
	// The panels.
	static final private Vector<JPanel> _panels = new Vector<JPanel>();
	
	// The tabs.
	static final private JTabbedPane _tabs = new JTabbedPane(JTabbedPane.TOP);
	
	// Default zoom level.
	static final public double DEFAULT_ZOOM = 60000;
	
	static final public double DEFAULT_AREA_HEIGHT = 20;
	
	static final public double DEFAULT_FACTOR = 3;
	
	public FunctionalityPanel(WorldWindow wwd, ControllerInterface controller,
			AnnotationLayer layer)
    {
        super(new BorderLayout());
        this._wwd = wwd;
        this._controller = controller;
        this._datesLayer = layer;
        this._layers = new HashMap<String, AirspaceLayer>();
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.setPreferredSize(new Dimension(250, 320));
        
        JPanel loginInnerPanel = new LoginPanel(this._wwd, this._controller, this._layers);
        FunctionalityPanel._tabs.add("Login", loginInnerPanel);
        
        // Creating the panels.
        JPanel benchmarksInnerPanel = new BenchmarksPanel(this._wwd, this._controller);
        FunctionalityPanel._panels.add(benchmarksInnerPanel);
        JPanel zoomingInnerPanel = new ZoomingPanel(this._wwd);
        FunctionalityPanel._panels.add(zoomingInnerPanel);
        JPanel coordinatesPanel = new CoordinatesPanel(this._wwd, this._controller);
        FunctionalityPanel._panels.add(coordinatesPanel);
        JPanel historyPanel = new HistoryPanel(this._wwd, this._controller, this._layers, this._datesLayer);
        FunctionalityPanel._panels.add(historyPanel);
        //JPanel showHidePanel = new ShowHidePanel(this._wwd);
        //FunctionalityPanel._panels.add(showHidePanel);
        JPanel logoutInnerPanel = new LogoutPanel(this._wwd, this._controller);
        FunctionalityPanel._panels.add(logoutInnerPanel);
        
        FunctionalityPanel._panels.add(loginInnerPanel);
        
        // Adding the tabs to the main panel.
        this.add(FunctionalityPanel._tabs);
        this.setBorder(
                new CompoundBorder(BorderFactory.createEmptyBorder(),
    					new TitledBorder("Actions")));
    }
	
	public static void updateBenchmarksPanel(){
		((BenchmarksPanel)FunctionalityPanel._panels.get(0)).updateComboBox();
	}
	
	public static void updateHistoryPanel(){
		((HistoryPanel)FunctionalityPanel._panels.get(3)).updateComboBox();
	}
	
	public static void removeDatesAnnotation(){
		((HistoryPanel)FunctionalityPanel._panels.get(3)).removeDatesAnnotation();
	}
	
	public static void changePanels(){
		// Deleting login panel.
		FunctionalityPanel._tabs.removeAll();
		// Adding the panels to the tabs.
        FunctionalityPanel._tabs.add("Benchmarks", FunctionalityPanel._panels.get(0));
        FunctionalityPanel._tabs.add("Zooming", FunctionalityPanel._panels.get(1));
        FunctionalityPanel._tabs.add("Coordinates", FunctionalityPanel._panels.get(2));
        FunctionalityPanel._tabs.add("History", FunctionalityPanel._panels.get(3));
        //FunctionalityPanel._tabs.add("Show/Hide objects", FunctionalityPanel._panels.get(4));
        FunctionalityPanel._tabs.add("Logout", FunctionalityPanel._panels.get(4));
        
        // Updating the panels.
        FunctionalityPanel.updateBenchmarksPanel();
        FunctionalityPanel.updateHistoryPanel();
	}
	
	public static void changeToLoginPanel(){
		// Deleting panels.
		FunctionalityPanel._tabs.removeAll();
		
		// Remove annotations.
		FunctionalityPanel.removeDatesAnnotation();
		
		// Remove data layers.
		//FunctionalityPanel.removeLayers();
		
		// Add the login panel.
		FunctionalityPanel._tabs.add("Login", FunctionalityPanel._panels.get(5));
	}

}
